//
//  ZQBasePage.m
//  jianzhik
//
//  Created by huami on 2018/8/28.
//  Copyright © 2018年 WKWebView. All rights reserved.
//

#import <Realm/Realm.h>
#import "ZQBasePage.h"
#import "UIBarButtonItem+item.h"
#import "ZQSaveModels.h"
@interface ZQBasePage ()
/** model */
@property (nonatomic, strong) ZQSaveModels *saveLoginModel;

@end

@implementation ZQBasePage
- (ZQSaveModels *)saveLoginModel
{
    if (!_saveLoginModel) {
        _saveLoginModel = [[ZQSaveModels alloc] init];
    }
    return _saveLoginModel;
}

- (UIButton *)footButton
{
    if (!_footButton) {
        UIButton *footButton = [[UIButton alloc] init];
        footButton.frame = CGRectMake(0, UScreenHeight - 47 - kBottomLineHeight, UScreenWidth, 47);
        [footButton setBackgroundColor: [UIColor stringToColor:@"#6eabf6"]];
        [footButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [footButton setTitle:@"添加管理员" forState:UIControlStateNormal];
        footButton.titleLabel.font = [UIFont systemFontOfSize:19];
        [self.view addSubview:footButton];
        _footButton = footButton;
    }
    return _footButton;
}

- (void)viewDidLoad {
    [super viewDidLoad];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)setNavBar
{
    UINavigationBar *navBar = self.navigationController.navigationBar;
    //    // 设置导航栏标题字体大小
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = [UIFont boldSystemFontOfSize:18];
    attrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
    [navBar setTitleTextAttributes:attrs];
    [navBar setBarTintColor:[UIColor blackColor]];
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setTitle:@"" forState:UIControlStateNormal];
    [backBtn setImage:[UIImage imageNamed:@"item_back"] forState:UIControlStateNormal];
    [backBtn setImage:[UIImage imageNamed:@"item_back"] forState:UIControlStateHighlighted];
    [backBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [backBtn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    [backBtn addTarget:self action:@selector(popVC) forControlEvents:UIControlEventTouchUpInside];
    [backBtn sizeToFit];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:backBtn];
}

- (void)popVC
{
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)saveData:(ZQSaveModels *)zqLoginModels
{
    NSString* doucmentpath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)firstObject];
    NSLog(@"沙盒路径%@",doucmentpath);
    NSLog(@"存数据");

        RLMResults<ZQSaveModels *> *temp = [ZQSaveModels objectsWhere:@"Username = %@",zqLoginModels.Username];
        NSLog(@"%@",temp);
        if (temp.count == 0) {
            [[RLMRealm defaultRealm] transactionWithBlock:^{
                [[RLMRealm defaultRealm] addObject:zqLoginModels];
            }];
        } else {
            NSLog(@"数据库已经有该条数据不需要再存了");
        }
}

- (ZQSaveModels *)queryDataWithValue:(NSString *)queryValue
{
    
    NSLog(@"查询");
    self.saveLoginModel = [[ZQSaveModels objectsWhere:@"Username = %@",queryValue]firstObject];
    NSLog(@"取出来的数据%@",self.saveLoginModel);
//    NSString *querDataStr = [NSString stringWithFormat:@"Username = admiin"];
//    [[ZQSaveModels objectsWhere:@"Username=%@",admin]firstObject];
////    _messreaml=[[ZQSaveModels objectsWhere:querDataStr] firstObject];
//    NSLog(@"取出来的数据%@",[[ZQSaveModels objectsWhere:querDataStr] firstObject]);
    return  self.saveLoginModel;
}

- (void)changeDataQueryDataWithValue:(id)queryValue  model:(ZQSaveModels *)saveModel

{
    NSLog(@"改数据");
    
    RLMRealm *realm = [RLMRealm defaultRealm];

//    self.saveLoginModel = [[ZQSaveModels objectsWhere:@"Username = %@",queryValue] firstObject];
//    NSLog(@"取出来的数据%@",self.saveLoginModel);
//    [Car createOrUpdateInRealm:realm withValue:@{@"id": @1, @"price": @9000.0f}];
//    [[RLMRealm defaultRealm] beginWriteTransaction];
 
    if (queryValue) {
        [realm beginWriteTransaction];
        
        [ZQSaveModels createOrUpdateInRealm:realm withValue:queryValue];
        // book 对象的 `title` 属性仍旧保持不变
        [realm commitWriteTransaction];
//        self.saveLoginModel=[[ZQSaveModels objectsWhere:@"Username =%@",queryValue]firstObject];
////        NSLog(@"取出来的数据%@",_messreaml.sex);
//        //改数据
//
//            [[RLMRealm defaultRealm] transactionWithBlock:^{
//                self.saveLoginModel.Token=@"白美";
//                //再存
//                [[RLMRealm defaultRealm] addObject:self.saveLoginModel];
//            }];
        
    } else {

        [realm transactionWithBlock:^{
            [realm addOrUpdateObject:saveModel];
        }];

    }
//    [[RLMRealm defaultRealm] commitWriteTransaction];

//    [[RLMRealm defaultRealm] addOrUpdateObject:saveModel];
//        [[RLMRealm defaultRealm] transactionWithBlock:^{
////            _messreaml.nickName=@"白富美";
//            self.saveLoginModel.Id = saveModel.Id;
//                        //再存
////            self.saveLoginModel = saveModel;
//            [[RLMRealm defaultRealm] addObject:self.saveLoginModel];
//        }];
}
//    //改数据
//    if (_messreaml.sex) {
//        [[RLMRealm defaultRealm] transactionWithBlock:^{
//            _messreaml.nickName=@"白富美";
//            //再存
//            [[RLMRealm defaultRealm] addObject:_messreaml];
//        }];
//    }
    
    //        dispatch_async(dispatch_queue_create("background", 0), ^{//开启线程存
    //            @autoreleasepool {
    //
    //                Messagereaml* messreaml=[[Messagereaml objectsWhere:@"otherDwID = %@",str] firstObject];
    //                RLMRealm *realm = [RLMRealm defaultRealm];
    //                [realm beginWriteTransaction];
    //                messreaml.sex=@"女";
    //                [realm commitWriteTransaction];
    //
    //            }
    //        });//更新某一条数据 这个写法与上面的写法是一样的


//-(void)delegatedata{
//
//    NSLog(@"删除某一条数据");
//    NSString* str=@"15118130757";
//    RLMResults<Messagereaml *> *temp = [Messagereaml objectsWhere:@"otherDwID = %@",str];
//    RLMRealm *realm = [RLMRealm defaultRealm];
//    [realm beginWriteTransaction];
//    [realm deleteObjects:temp];//删除某条数据
//    [realm commitWriteTransaction];
//
//}
//
//-(void)delegatealldata{
//    NSLog(@"删除所有数据");
//
//    RLMRealm *realm = [RLMRealm defaultRealm];
//    [realm beginWriteTransaction];
//    [realm deleteAllObjects];//删除所有数据
//    [realm commitWriteTransaction];
//
//
//}
//
//-(void)getalldata{
//
//    RLMResults<Messagereaml *> *temp = [Messagereaml allObjects];
//    NSLog(@"查询所有数据%@",temp);
//}

@end
